package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.EnterpriseModify;
import com.innovations.inn.domain.request.RequestEnterpriseInfoAll;
import com.innovations.inn.domain.response.ResponseEnterpriseModifyInfo;
import com.innovations.inn.domain.response.ResponseEnterpriseModifyInfoAll;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.service.IEnterpriseModifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static com.innovations.common.utils.MyBeanUtils.copyProperties;

/**
 * 企业信息修改记录Controller
 *
 * @author dp
 * @date 2024-11-08
 */
@Api(tags = "企业信息修改记录")
@RestController
@RequestMapping("/inn/enterpris/modify")
public class EnterpriseModifyController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    private IEnterpriseModifyService enterpriseModifyService;

    /**
     * 查询企业信息修改记录列表
     */
    @ApiOperation("查询企业信息修改记录列表")
    @PreAuthorize("@ss.hasPermi('inn:modify:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseModify enterpriseModify) {
        startPage();
        List<EnterpriseModify> list = enterpriseModifyService.selectEnterpriseModifyList(enterpriseModify);
        return getDataTable(list);
    }

    /**
     * 查询企业信息修改记录详细列表
     */
    @ApiOperation("查询企业详细信息列表")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:list')")
    @GetMapping("/list/info")
    public TableDataInfo listInfo(EnterpriseModify enterpriseModify) {
        startPage();
        List<ResponseEnterpriseModifyInfo> list = enterpriseModifyService.selectEnterpriseModifyListInfo(enterpriseModify);
        return getDataTable(list);
    }

    /**
     * 查询企业信息修改记录细信息列表
     */
    @ApiOperation("查询企业及资质详细信息列表")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:list')")
    @GetMapping("/list/info/all")
    public TableDataInfo listInfoAll(EnterpriseModify enterpriseModify) {
        startPage();
        List<ResponseEnterpriseModifyInfoAll> list = enterpriseModifyService.selectEnterpriseModifyListInfoAll(enterpriseModify);
        return getDataTable(list);
    }

    /**
     * 查询企业信息修改记录细信息列表合计
     */
    @ApiOperation("查询企业及资质详细信息列表合计")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:list')")
    @GetMapping("/list/info/all/count")
    public TableDataInfo listInfoAllCount(EnterpriseModify enterpriseModify) {
        startPage();
        List<ResponseItemCount> listCount = enterpriseModifyService.selectEnterpriseModifyListInfoAllCount(enterpriseModify);
        return getDataTable(listCount);
    }

    /**
     * 导出企业信息修改记录列表
     */
    @PreAuthorize("@ss.hasPermi('inn:modify:export')")
    @Log(title = "企业信息修改记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterpriseModify enterpriseModify) {
        List<EnterpriseModify> list = enterpriseModifyService.selectEnterpriseModifyList(enterpriseModify);
        ExcelUtil<EnterpriseModify> util = new ExcelUtil<EnterpriseModify>(EnterpriseModify.class);
        util.exportExcel(response, list, "企业信息修改记录数据");
    }

    /**
     * 获取企业信息修改记录详细信息
     */
    @ApiOperation("获取企业信息修改记录详细信息")
    @PreAuthorize("@ss.hasPermi('inn:modify:query')")
    @GetMapping(value = "/{enterpriseId}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") Long enterpriseId) {
        return success(enterpriseModifyService.selectEnterpriseModifyByEnterpriseId(enterpriseId));
    }

    /**
     * 新增企业信息修改记录
     */
    @ApiOperation("新增企业信息修改记录")
    @PreAuthorize("@ss.hasPermi('inn:modify:add')")
    @Log(title = "企业信息修改记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody EnterpriseModify enterpriseModify) {
        if (enterpriseModify.getUserId() == null) {
            enterpriseModify.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        }
        Long userId = enterpriseModify.getUserId();
        enterpriseModifyService.completeData(enterpriseModify, userId, 1);
        if (!enterpriseModifyService.checkEnterpriseModifyUnique(enterpriseModify)) {
            return error("存在未审核的修改记录");
        }
        return toAjax(enterpriseModifyService.insertEnterpriseModify(enterpriseModify));
    }

    /**
     * 新增企业修改记录及资质信息
     */
    @ApiOperation("新增企业及资质信息")
    @PreAuthorize("@ss.hasPermi('inn:enterprise:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping("/info")
    public AjaxResult addAll(HttpServletRequest request, @RequestBody RequestEnterpriseInfoAll enterpriseInfoAll) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterpriseInfoAll.setUserId(userId);
        // 企业信息修改记录
        EnterpriseModify enterpriseModify = new EnterpriseModify();
        enterpriseModify = copyProperties(enterpriseInfoAll, EnterpriseModify.class);
        enterpriseModifyService.completeData(enterpriseModify, userId, 2);
        // 企业详细信息
        enterpriseInfoAll = copyProperties(enterpriseModify, RequestEnterpriseInfoAll.class);

        return toAjax(enterpriseModifyService.insertEnterpriseAll(enterpriseInfoAll));
    }

    /**
     * 修改企业信息修改记录
     */
    @ApiOperation("修改企业信息修改记录")
    @PreAuthorize("@ss.hasPermi('inn:modify:edit')")
    @Log(title = "企业信息修改记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody EnterpriseModify enterpriseModify) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterpriseModifyService.completeData(enterpriseModify, userId, 2);
        return toAjax(enterpriseModifyService.updateEnterpriseModify(enterpriseModify));
    }

    /**
     * 删除企业信息修改记录
     */
    @ApiOperation("删除企业信息修改记录")
    @PreAuthorize("@ss.hasPermi('inn:modify:remove')")
    @Log(title = "企业信息修改记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable Long[] enterpriseIds) {
        return toAjax(enterpriseModifyService.deleteEnterpriseModifyByEnterpriseIds(enterpriseIds));
    }
}
